/**=========================================================
 * Controller: SystemGroupController
  =========================================================*/
App.controller('SystemGroupController', ['$scope', 'toaster', 'ngDialog', 'Utils', 'HttpUtils', 'DialogUtils', 'SimpleUiGrid',
    function($scope, toaster, ngDialog, utils, httpUtils, dialogUtils, simpleUiGridService) {
        $scope.currentGroup = {};
        //===========================groupGrid=================================            
        var groupGridColumns = [
            {
                displayName: '群组名称',
                field: 'name'
            },
            {
                displayName: '群组类型',
                field: 'type'
            },
            {
                displayName: '自定义值',
                field: 'custom_value'
            },
            {
                displayName: '备注',
                field: 'remark'
            },
            {
                displayName: '状态',
                field: 'status',
                width: '80'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '150',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.selectUser(row.entity)" title="维护用户" class="btn btn-xs btn-success">\
                            <em class="fa fa-users"></em>\
                        </button>\
                        <button ng-click="grid.appScope.saveGroup(row.entity)" title="保存" class="btn btn-xs btn-info">\
                            <em class="fa fa-save"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteGroup(row.entity)" title="删除" class="btn btn-xs btn-danger">\
                            <em class="fa fa-trash"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
        
        $scope.queryParams = {namespace: 'SystemMapper.getAllGroups'};
        $scope.searchGroupGrid = function() {
            $scope.currentGroup = {};
            groupUserGrid.clear();
            groupGrid.draw($scope.queryParams);
        }; 

        var groupGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'groupGridOptions',
                pageOptionModel: 'groupGridPageOptions',
                gridApi: 'groupGridApi',
                columns: groupGridColumns,
                pageType: 'common',
                needExport: false,
                controllerSearchFn: $scope.searchGroupGrid,
                menus: [
                    {
                        title: '新增',
                        action: function ($event) {
                            groupGrid.addRow({status: '1'},0);
                        }
                    }
                ],
                registerApiFn: function(gridApi){
                    gridApi.selection.on.rowSelectionChanged($scope, function (row, event) {
                        $scope.currentGroup.id = row.entity.tid;
                        searchGroupUserGrid();
                    });
                }
            }
        );
       
        $scope.saveGroup = function(data){
            httpUtils.post('/sysGroup/save',data,
                function(result){
                    if (result.success){
                        if (utils.isEmpty(data.tid)){
                            data.tid = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        };
        
        $scope.deleteGroup = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function () {
                    httpUtils.post('/sysGroup/delete',data,
                        function(result){
                            if (result.success){
                                groupGrid.deleteRow(data);
                                groupUserGrid.clear();
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            );
        };
        
        $scope.refreshCache = function(){
             httpUtils.get('/sysGroup/refreshGroupUserCache',{},
                function(result){
                    if (result.success){
                        toaster.pop('success', '', '刷新成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '刷新失败');
                }
            );
        }
        
        //选择用户
        $scope.selectUser = function(data){
            ngDialog.openConfirm({
                template: 'selectUserDialog',
                className: 'ngdialog-theme-default',
                appendClassName: 'ngdialog-lg',
                scope:$scope,
                controller: ['$scope', function($scope) {
                    $scope.searchOtherUserSelectGrid(data.tid);
                    $scope.searchGroupUserSelectGrid(data.tid);
                }]
            }).then(function (value) {
                var userIDs = '',
                    selectedUsers = groupUserSelectGrid.getAll();
                
                for (var i = 0; i < selectedUsers.length; i++){
                    userIDs += selectedUsers[i].tid + ",";
                }
                
                httpUtils.post('/sysGroup/saveGroupUser',
                    {
                        groupID: $scope.currentGroup.id,
                        userIDs: userIDs
                    },
                    function(result){
                        if (result.success){
                            toaster.pop('success', '', '配置成功');
                            searchGroupUserGrid();
                        }else{
                            toaster.pop('error', '', result.msg);
                        }
                    },
                    function(result){
                        toaster.pop('error', '', '配置失败');
                    }
                );
            });
        }
        
        $scope.goLeft = function(){
            if (otherUserSelectGrid.getSelection().length > 0){
                for (var i = 0; i < otherUserSelectGrid.getSelection().length; i++){
                    groupUserSelectGrid.addRow(otherUserSelectGrid.getSelection()[i]);
                    otherUserSelectGrid.deleteRow(otherUserSelectGrid.getSelection()[i]);
                } 
            }   
        }
        
        $scope.goRight = function(){
            if (groupUserSelectGrid.getSelection().length > 0){
                for (var i = 0; i < groupUserSelectGrid.getSelection().length; i++){
                    otherUserSelectGrid.addRow(groupUserSelectGrid.getSelection()[i]);
                    groupUserSelectGrid.deleteRow(groupUserSelectGrid.getSelection()[i]);
                } 
            }
        }
        
//===========================groupUserGrid=================================        
        var groupUserGridColumns = [
            {
                displayName: '用户编号',
                field: 'code'
            },
            {
                displayName: '用户名称',
                field: 'cnname'
            },
            {
                displayName: '邮箱',
                field: 'email'
            },
            {
                displayName: '状态',
                field: 'status',
                width: '80'
            }
        ];
            
        var searchGroupUserGrid = function() {
            if (utils.isEmpty($scope.currentGroup.id)){
                groupUserGrid.clear();
            }else {
                var params = {
                    namespace: 'SystemMapper.getGroupUser',
                    groupID: $scope.currentGroup.id
                }

                groupUserGrid.draw(params);
            }
        }; 

        var groupUserGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'groupUserGridOptions',
                gridApi: 'groupUserGridApi',
                needExport: false,
                columns: groupUserGridColumns,
                needPage: false
            }
        );  
 //===========================groupUserSelectGrid=================================
        var groupUserSelectGridColumns = [
            {
                displayName: '用户编号',
                field: 'code'
            },
            {
                displayName: '用户名称',
                field: 'cnname'
            },
            {
                displayName: '状态',
                field: 'status',
                width: '80'
            }
        ];     
               
        $scope.searchGroupUserSelectGrid = function(id) {
            if (utils.isEmpty(id)){
                groupUserSelectGrid.clear();
            }else {
                var params = {
                    namespace: 'SystemMapper.getGroupUser',
                    groupID: id
                }

                groupUserSelectGrid.draw(params);
            }
        }; 

        var groupUserSelectGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'groupUserSelectGridOptions',
                gridApi: 'groupUserSelectGridApi',
                columns: groupUserSelectGridColumns,
                needPage: false,
                multiSelect: true
            }
        );       
 //===========================otherUserSelectGrid================================= 
        $scope.selectFilterParams = {namespace: 'SystemMapper.getOtherUser'};
        var otherUserSelectGridColumns = [
            {
                displayName: '用户编号',
                field: 'code'
            },
            {
                displayName: '用户名称',
                field: 'cnname'
            }
        ];                 
        $scope.searchOtherUserSelectGrid = function(id) {
            if (utils.isEmpty(id)){
                otherUserSelectGrid.clear();
            }else {
                $scope.selectFilterParams.groupID = id;

                otherUserSelectGrid.draw($scope.selectFilterParams);
            }
        }; 

        var otherUserSelectGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'otherUserSelectGridOptions',
                gridApi: 'otherUserSelectGridApi',
                columns: otherUserSelectGridColumns,
                needPage: false,
                multiSelect: true
            }
        );  
//===========================init================================= 
        $scope.searchGroupGrid();
    }
]);
